#include "StdAfx.h"
#include "XmlElement.h"

void XmlElement::Output(wostream &out, int iIndent){
	// Control the indental format
	for(int j  = 0;j < iIndent; ++ j) out << "  ";
	// CASE: "<Name "
	out << wLeftBra << this->GetName() <<  wWhiteSp;
	ATTRIBUTES::iterator it = attributes.begin();
	while(it != attributes.end()){
		out << it->first << "=" << wQuote << it->second << wQuote << wWhiteSp;
		++ it;
	}
	out << wRightBra << endl;
	vector<XmlChild*>::iterator itv = childElem.begin();
	while(itv != childElem.end()){
		(*itv)->Output(out, iIndent + 2);
		++ itv;
	}
	// Control the indental format
	for(int j  = 0;j < iIndent; ++ j) out << "  ";
	out << wLeftBra << wSlash << this->GetName() << wRightBra << endl;
}

bool XmlElement::AddElement(XmlChild* aChildElem){
	childElem.push_back(aChildElem);
	return true;
}

bool XmlElement::DelElement(XmlChild* aChildElem){
	vector<XmlChild*>::iterator it = find(childElem.begin(), childElem.end(), aChildElem);
	if(it == childElem.end()) 
		return false;
	vector<XmlChild*>::iterator itRet = childElem.erase(it);
	return true;
}

vector<XmlChild*>* XmlElement::GetChildElement(){
	return &childElem;
}

XmlChild* XmlElement::GetChildElementByAttr(wstring strAttrName, wstring strAttrValue){
	vector<XmlChild*>::iterator it = childElem.begin();
	while (it != childElem.end())
	{
		if((*it)->GetAttribute(strAttrName) == strAttrValue)
			return *it;
		++ it;
	}
	return NULL;
}

// Destruction~
XmlElement::~XmlElement(){
	vector<XmlChild*>::iterator itv = childElem.begin();
	while(itv != childElem.end()){
		delete (*itv);
		(*itv) = NULL;
		++ itv;
	}
}